package com.yin.myproject.lb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Eason on 2017/8/3.
 */
public class RoundRobinSample01 {
    private static Integer pos = 0;

    public static String getServer() {
        // 重建一个Map，避免服务器的上下线导致的并发问题
        Map<String, Integer> serverMap = new HashMap<String, Integer>();
        serverMap.putAll(IpMap.serverWeightMap);
        // 取得Ip地址List
        Set<String> keySet = serverMap.keySet();
        ArrayList<String> keyList = new ArrayList<String>();
        keyList.addAll(keySet);
        String server;
        synchronized (pos) {
            System.out.println("pos == " + pos);
            if (pos >= keySet.size())
                pos = 0;
            server = keyList.get(pos);
            pos++;
        }
        return server;
    }
}
